package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.qbdms.dao.WQbdEmergentPassDao;
import com.ccp.dev.qbdms.dao.WQbdOutsourceCheckDao;
import com.ccp.dev.qbdms.dao.WQbdProductCertificateInfoDao;
import com.ccp.dev.qbdms.dao.WQbdUnderqualiTrialDao;
import com.ccp.dev.qbdms.dao.WQbdUnzeroWarningDao;
import com.ccp.dev.qbdms.model.WQbdUnderqualiTrial;
import com.google.common.collect.Maps;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

/**
 * ClassDescribe:
 *
 * @author :milihao
 */
@Service
public class WQbdQualityCheckStatisticsService {


	@Resource
	private WQbdOutsourceCheckDao wQbdOutsourceCheckDao;
	@Resource
	private WQbdUnderqualiTrialDao wQbdUnderqualiTrialDao;
	@Resource
	private WQbdEmergentPassDao wQbdEmergentPassDao;
	@Resource
	private WQbdProductCertificateInfoDao wQbdProductCertificateInfoDao;

	public Map<String,Object> getChartData(QueryFilter queryFilter) {
		String statisticalMethods = queryFilter.get("statisticalMethods").toString();
		String year = queryFilter.get("year").toString();
		try {
			queryFilter.addFilter("startTime",
					DateUtils.parseDate(year + "-01-01 00:00:00", "yyyy-MM-dd HH:mm:ss"));
			queryFilter.addFilter("endTime",
					DateUtils.parseDate(year + "-12-31 23:59:59", "yyyy-MM-dd HH:mm:ss"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Map<String, Object> returnDatMap = null;
		switch (statisticalMethods) {
			case "外协外购产品检验":
				returnDatMap = packageData(wQbdOutsourceCheckDao.getStatisticalChartData(queryFilter));
				break;
			case "不合格品审理":
				returnDatMap = packageData(wQbdUnderqualiTrialDao.getStatisticalChartData(queryFilter));
				break;
			case "紧急例外放行":
				returnDatMap = packageData(wQbdEmergentPassDao.getStatisticalChartData(queryFilter));
				break;
			case "产品证明书":
				returnDatMap = packageData(
						wQbdProductCertificateInfoDao.getStatisticalChartData(queryFilter));
				break;
			default:
				break;
		}
		return returnDatMap;


	}

	protected Map<String, Object> packageData(List<Map<String, Object>> dataMapList) {
		if (null == dataMapList || dataMapList.size() == 0) {
			return null;
		}
		List<Object> seriesData = Lists.newArrayList();
		List<String> legendData = Lists.newArrayList();
		Map<String, Object> returnDataMp = Maps.newHashMap();
		for (Map<String, Object> map : dataMapList) {
			seriesData.add(Integer.parseInt(map.get("COUNT").toString()));
			legendData.add(map.get("NAME").toString());
		}
		returnDataMp.put("seriesData", seriesData);
		returnDataMp.put("legendData", legendData);
		return returnDataMp;


	}
}
